<?php

include_once("Model/user_model.php");

class User extends MyController
{    
    public $model = null;
    
    public function __construct()
    {
        $this->model = new User_Model();
    }
    
    public function getAll()
    {
        $this->login();
    }
    
    public function showLogin($note)
    {
        $notice = $note;
        include("View/header.php");
        include("View/user/login.php");
        include("View/footer.php");
    }
    
    public function login()
    {
        if(isset($_SESSION['isLogin'])){
            header("location: index.php?control=tinhthanh");
        }
        else
        {
            if(isset($_POST['submit']))
            {            
                $key=substr($_SESSION['key'],0,5);
                $number = $_REQUEST['number'];
                if($number==$key)
                { 
                    if($this->model->checkExist("login", $_POST['username'], $_POST['password']))
                    {
                        $row = $this->model->getUser('username', $_POST['username']);
                        $_SESSION['id'] = $row['id'];
                        $_SESSION['username'] = $row['username'];
                        $_SESSION['isLogin'] = true;
                        header("location: index.php?control=tinhthanh");
                    }
                    else
                    {
                        $notice = Message::$loginFail;
                        $this->showLogin($notice);
                    }
                }
                else
                {                
                    $notice = Message::$wrongCaptcha;
                    $this->showLogin($notice);
                }
            }
            else
            {
                $this->showLogin("");
            }
        }
    }

    
    public function logout()
    { 
        session_destroy();
        header("location: index.php"); 
    }
}

?>